﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lilium.LParse
{
    public static class PrimParsers
    {
        public static Parser<TToken, TToken> AnyToken<TToken>()
        {
            return state =>
                {
                    if (!state.Eof)
                    {
                        var current = state.Current;
                        state.Next();
                        return current;
                    }
                    else
                    {
                        state.Fail("any token");
                        return default(TToken);
                    }
                };
        }

        public static Parser<TToken, TResult> Eof<TToken, TResult>()
        {
            return state =>
                {
                    if (!state.Eof)
                        state.Fail("[eof]");
                    else
                    {
                        state.DiscardErrors();
                    }

                    return default(TResult);
                };
        }

        public static Parser<TToken, TResult> Fail<TToken, TResult>(string exceptedValue)
        {
            return state =>
                {
                    state.Fail(exceptedValue);
                    return default(TResult);
                };
        }

        public static Parser<TToken, object> SkipWhile<TToken>(Func<TToken, bool> predicate)
        {
            return state =>
                {
                    while (!state.Eof && predicate(state.Current))
                        state.Next();

                    return null;
                };
        }
    }
}